package com.ad.pet.app.address;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.utils.ui.DialogUtil;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.PermissionManager;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.address.Address;
import com.ad.pet.models.remote.address.Province;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * 283 930 004 627
 * Created by SEELE on 2018/6/25.
 */

public class AddressListActivity extends AppToolbarActivity implements AddressContract.View {
    @Inject
    PermissionManager mPermissionManager;
    @Inject
    RxDisposable mRxDisposable;
    @BindView(R.id.recycler_address)
    RecyclerView mRecyclerAddress;
    @BindView(R.id.relative_add)
    RelativeLayout mBtnCommit;
    @Inject
    AddressPresenter mAddressPresenter;
    private AddressAdapter mAddressAdapter;
    private List<Address> mAddressList;
    private Address mAddress;
    private boolean mIsCheck;//用于判断是从购物处点开的还是从个人中心点开的，从购物车点开的是false，从个人中心点开的是true

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle("地址管理");
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mAddress = (Address) getIntent().getSerializableExtra("address");
        mIsCheck = getIntent().getBooleanExtra(Constant.IS_CHECK, false);
        mPermissionManager.setRxPermissions(new RxPermissions(this));
        mAddressPresenter.onCreate(this);
        mAddressPresenter.getAddressList();


        mAddressAdapter = new AddressAdapter(null, mAddress);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerAddress.setLayoutManager(linearLayoutManager);
        mRecyclerAddress.addItemDecoration(new SimpleDividerDecoration(mActivity, 3, R.color.bg_color));
        mAddressAdapter.bindToRecyclerView(mRecyclerAddress);
        mRecyclerAddress.setAdapter(mAddressAdapter);


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mBtnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Navigation.goAddressDetail(mActivity, null, 2,mIsCheck);
            }
        });

        mAddressAdapter.setOnClickEditLinstener(new AddressAdapter.OnClickEditLinstener() {
            @Override
            public void onEdit(int position) {
                if (mAddressList!=null)
                Navigation.goAddressDetail(mActivity, mAddressList.get(position),2,mIsCheck);
            }

            @Override
            public void onDelete(final int position) {
                DialogUtil.createDialog(mActivity, "", getString(R.string.sure_delete_item_address), true, getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                }, getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAddressPresenter.delAddress(mAddressAdapter.getData().get(position).getId());
                    }
                }).show();
            }

            @Override
            public void onDefult(int position) {
//                mAddressPresenter.updataAddress("default",mAddressAdapter.getItem(position).getId(),"","","","","","","","","","",mAddressAdapter.getItem(position).getIs_default()==0?1:0);
            }

        });
        mAddressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Address> addresses = mAddressAdapter.getData();
                if (mIsCheck) {

                } else {
                    Intent intent = new Intent();
                    intent.putExtra("address", addresses.get(position));
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
        });

        mRxDisposable.add(RxBus.getDefault().toObservable(Address.class)
                .subscribe(new Action1<Address>() {
                    @Override
                    public void call(Address s) {
                        finish();
                    }
                }));
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 2:
                mAddressPresenter.getAddressList();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        mRxDisposable.clear();
        super.onDestroy();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {
//        if (code==0||code==201){
//            mAddressAdapter.setEmptyView(R.layout.layout_empty_address);
//        }

            ToastUtil.getToastUtil().showShort(msg);

    }

    @Override
    public void getAddresssListSuccess(List<Address> addressList) {
        mAddressList = addressList;
        if (addressList == null || addressList.size() == 0) {
            mAddressAdapter.setEmptyView(R.layout.layout_empty_address);
        }
        Collections.sort(addressList);
        mAddressAdapter.setNewData(addressList);

    }

    @Override
    public void getUpdateAddressSuccess() {
            mAddressPresenter.getAddressList();
    }

    @Override
    public void delAddressSuccess() {
        mAddressPresenter.getAddressList();
    }

    @Override
    public void getCountryProvenceSuccess(List<Province> list) {

    }

}
